package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.SellLevelService;
import com.junzhiit.nengji.cms.web.exception.SellLevelNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SellLevelVO;
import com.junzhiit.nengji.dao.SellLevelDAO;
import com.junzhiit.nengji.model.SellLevel;

@Service
public class SellLevelServiceImpl extends BaseServiceImpl implements SellLevelService{

	@Autowired
	private SellLevelDAO sellLevelDAO;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, SellLevelVO sellLevelVO) {
		SellLevel sellLevel=new SellLevel();
		BeanUtils.copyProperties(sellLevelVO, sellLevel);
		
		List<SellLevel> list=sellLevelDAO.findAll(sellLevel,  dtp.getStart(), dtp.getLength());
		int totalCount = sellLevelDAO.getTotalCount(sellLevel);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO findById(String id) {
		ResultVO result = new ResultVO();
		SellLevel sellLevel = sellLevelDAO.query(id);
		if(sellLevel == null ){
			throw new SellLevelNotFoundException("分润类别不存在");
		}
		result.setData(sellLevel);
		return result;
	}

	@Override
	public ResultVO add(SellLevelVO sellLevelVO) {
		ResultVO result = new ResultVO();
		SellLevel sellLevel=new SellLevel();
		BeanUtils.copyProperties(sellLevelVO, sellLevel);
		sellLevelDAO.insert(sellLevel);
		return result;
	}

	@Override
	public ResultVO delete(String id) {
		ResultVO result = new ResultVO();
		SellLevel sellLevel = sellLevelDAO.query(id);
		if(sellLevel == null ){
			throw new SellLevelNotFoundException("分润类别不存在");
		}
		sellLevelDAO.updateToDelete(id);
		return result;
	}

	@Override
	public ResultVO update(SellLevelVO sellLevelVO) {
		ResultVO result = new ResultVO();
		SellLevel sellLevel=sellLevelDAO.query(sellLevelVO.getId());
		if(sellLevel == null){
			throw new SellLevelNotFoundException("分润类别不存在");
		}
		BeanUtils.copyProperties(sellLevelVO, sellLevel);
		
		sellLevelDAO.update(sellLevel);
		return result;
	}

	@Override
	public ResultVO getBenefitMax() {
		ResultVO result = new ResultVO();
		Integer max=sellLevelDAO.getSellLevelMax();
		SellLevel sellLevel = new SellLevel();
		sellLevel.setLevel(max+1);
		result.setData(sellLevel);
		return result;
	}
}
